package cn.iocoder.yudao.module.ai.controller.admin.operationscene;

import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.framework.exception.BusinessException;
import cn.iocoder.yudao.module.ai.constant.DicConstant;
import cn.iocoder.yudao.module.ai.controller.admin.operationalarm.vo.OperationAlarmRespVO;
import cn.iocoder.yudao.module.ai.service.operationendcompany.OperationEndCompanyService;
import cn.iocoder.yudao.module.infra.util.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.operationscene.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.operationscene.OperationSceneDO;
import cn.iocoder.yudao.module.ai.service.operationscene.OperationSceneService;

@Tag(name = "管理后台 - 运营中心-场景监控")
@RestController
@RequestMapping("/ai/operation-scene")
@Validated
public class OperationSceneController {

    @Resource
    private OperationSceneService operationSceneService;
    @Resource
    private OperationEndCompanyService operationEndCompanyService;

//    @PostMapping("/create")
//    @Operation(summary = "创建运营中心-场景监控")
//    @PreAuthorize("@ss.hasPermission('ai:operation-scene:create')")
//    public CommonResult<Long> createOperationScene(@Valid @RequestBody OperationSceneSaveReqVO createReqVO) {
//        return success(operationSceneService.createOperationScene(createReqVO));
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新运营中心-场景监控")
//    @PreAuthorize("@ss.hasPermission('ai:operation-scene:update')")
//    public CommonResult<Boolean> updateOperationScene(@Valid @RequestBody OperationSceneSaveReqVO updateReqVO) {
//        operationSceneService.updateOperationScene(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除运营中心-场景监控")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('ai:operation-scene:delete')")
//    public CommonResult<Boolean> deleteOperationScene(@RequestParam("id") Long id) {
//        operationSceneService.deleteOperationScene(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得运营中心-场景监控")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:operation-scene:query')")
    public CommonResult<OperationSceneRespVO> getOperationScene(@RequestParam("id") Long id) {
        OperationSceneDO operationScene = operationSceneService.getOperationScene(id);
        OperationSceneRespVO respVO = BeanUtils.toBean(operationScene, OperationSceneRespVO.class);
        if(null != respVO){
            respVO.setCompanyName(operationEndCompanyService.getCompanyName(respVO.getCompanyId()));
            respVO.setIndustryAffiliationName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_Industry_affiliation, respVO.getIndustryAffiliation()));
            respVO.setSceneClassificationName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_scene_classification, respVO.getSceneClassification()));
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得运营中心-场景监控分页")
    @PreAuthorize("@ss.hasPermission('ai:operation-scene:query')")
    public CommonResult<ScenePageResult<OperationSceneRespVO>> getOperationScenePage(@Valid OperationSceneSimplePageReqVO simplePageReqVO) {
        OperationScenePageReqVO pageReqVO = new OperationScenePageReqVO();
        org.springframework.beans.BeanUtils.copyProperties(simplePageReqVO, pageReqVO);
        if(StringUtils.isNotBlank(simplePageReqVO.getCompanyId())){
            List<String> ids = operationEndCompanyService.getSelfAndSubCompanyIds(simplePageReqVO.getCompanyId());
            pageReqVO.setCompanyIds(ids);
            if(CollectionUtils.isEmpty(ids)){
                throw new BusinessException("请选择正确的公司");
            }
        }
        // 查询统计信息
        OperationSceneRespCountVO operationSceneRespCountVO = operationSceneService.countOperationScene(pageReqVO.getCompanyIds(), simplePageReqVO);
        PageResult<OperationSceneDO> pageResult = operationSceneService.getOperationScenePage(pageReqVO);
        PageResult<OperationSceneRespVO> respVOPageResult = BeanUtils.toBean(pageResult, OperationSceneRespVO.class);
        if(null != respVOPageResult && null != respVOPageResult.getList()){
            Map<String, String> companyIdAndNameMap = operationEndCompanyService.getCompanyIdAndNameMap(respVOPageResult.getList().stream().map(OperationSceneRespVO::getCompanyId).collect(Collectors.toList()));
            respVOPageResult.getList().forEach(item -> {
                item.setCompanyName(companyIdAndNameMap.get(item.getCompanyId()));
                item.setIndustryAffiliationName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_Industry_affiliation, item.getIndustryAffiliation()));
                item.setSceneClassificationName(DictFrameworkUtils.getDictDataLabel(DicConstant.key_operation_scene_classification, item.getSceneClassification()));
            });
        }
        ScenePageResult<OperationSceneRespVO> scenePageResult = new ScenePageResult<>(respVOPageResult.getList(), respVOPageResult.getTotal(), operationSceneRespCountVO);
        return success(scenePageResult);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出运营中心-场景监控 Excel")
//    @PreAuthorize("@ss.hasPermission('ai:operation-scene:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportOperationSceneExcel(@Valid OperationScenePageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<OperationSceneDO> list = operationSceneService.getOperationScenePage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "运营中心-场景监控.xls", "数据", OperationSceneRespVO.class,
//                        BeanUtils.toBean(list, OperationSceneRespVO.class));
//    }

}